package server

import (
	"errors"
	"zk1/common/models"
	"zk1/common/proto/goods"
)

func Create(in *goods.CreateRequest) (*goods.CreateResponse, error) {
	g := models.Goods{
		GoodsDescription: in.GoodsDescription,
		GoodsCode:        int(in.GoodsCode),
		GoodsPrice:       int(in.GoodsPrice),
		SalesVolume:      int(in.SalesVolume),
		Classification:   int(in.Classification),
		Status:           int(in.Status),
	}
	err := g.Create()
	if err != nil {
		return nil, errors.New("服务器内部错误")
	}
	return &goods.CreateResponse{GoodsId: int64(g.ID)}, nil
}

func List(in *goods.ListRequest) (*goods.ListResponse, error) {
	var GoodsList []*goods.List
	list, err := models.List()
	if err != nil {
		return nil, err
	}
	for _, g := range list {
		goodsList := &goods.List{
			GoodsId:          int64(g.ID),
			GoodsDescription: g.GoodsDescription,
			GoodsCode:        int64(g.GoodsCode),
			GoodsPrice:       int64(g.GoodsPrice),
			SalesVolume:      int64(g.SalesVolume),
			Classification:   int64(g.Classification),
			Status:           int64(g.Status),
		}
		GoodsList = append(GoodsList, goodsList)
	}
	return &goods.ListResponse{List: GoodsList}, nil
}
